package heima.lm.oschina.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.base.BaseActivity;
import heima.lm.oschina.constant.HostTab;


public class MainActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.nav)
    NavigationView mNav;
    @BindView(R.id.main_drawer)
    DrawerLayout mMainDrawer;
    @BindView(R.id.tabhost)
    FragmentTabHost mTabHost;
    @BindView(R.id.iv_quick_option)
    ImageButton mImageButton;
    private long exitTime;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BaseApplication.mActivityList.add(this);
        ButterKnife.bind(this);
        initView();
        intiEvent();
    }

    private void initView() {
        setSupportActionBar(mToolbar);
        //设置DrawerLayout和ToolBar的联动
        mToolbar.setNavigationIcon(R.drawable.ic_drawer);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mMainDrawer, mToolbar, R.string.open, R.string.close);
        toggle.syncState();
        mMainDrawer.addDrawerListener(toggle);
        //初始化FragmentTabHost
        initTabHost();
    }

    private void initTabHost() {
        mTabHost.setup(this, getSupportFragmentManager(), R.id.fl_content);
        mTabHost.getTabWidget().setDividerDrawable(null);
        HostTab[] tabs = HostTab.values();
        for (int i = 0; i < tabs.length; i++) {
            HostTab tab = tabs[i];
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(this.getResources().getString(tab.getResName()));
            View indicator = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.tab_indicator, null);
            TextView tabTitle = (TextView) indicator.findViewById(R.id.tab_title);
            TextView tabMes = (TextView) indicator.findViewById(R.id.tab_mes);
            Drawable drawable = this.getResources().getDrawable(
                    tab.getResIcon());
            //设置TextView上下左右的绘图
            tabTitle.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null,
                    null);
            if (i == 2) {
                //当是正中间的标签，则隐藏 ，显示ImageView
                indicator.setVisibility(View.INVISIBLE);
            }
            tabTitle.setText(getString(tab.getResName()));
            //设置标签的指示器，是一个View
            tabSpec.setIndicator(indicator);
            mTabHost.addTab(tabSpec, tab.getClzz(), null);
        }
    }


    private void intiEvent() {
        mNav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_quest:

                        break;
                    case R.id.nav_open_soft:

                        break;
                    case R.id.nav_blog:

                        break;
                    case R.id.nav_git:

                        break;
                }
                return false;
            }
        });
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {

            }
        });
        mImageButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent intent = new Intent(MainActivity.this,LogActivity.class);
                startActivity(intent);
                return true;
            }
        });
        mImageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "图片", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private HostTab searchTab(String tabId) {
        for (HostTab hostTab : HostTab.values()) {
            if (TextUtils.equals(BaseApplication.mApp.getString(hostTab.getResName()), tabId)) {
                return hostTab;
            }
        }
        return null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search_content:
                //搜索
                break;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK &&
                event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                BaseApplication.exit();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private Bundle createBundle(String key, String value) {
        Bundle bundle = new Bundle();
        bundle.putString(key, value);
        return bundle;
    }
}

